package com.example.exchangeapp.yy.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.exchangeapp.R;
import com.example.exchangeapp.yy.entity.GoodVo2;
import com.example.exchangeapp.yy.request.HttpRequestUtils;
import com.example.exchangeapp.yy.utils.AlertDialogUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SendFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SendFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private List<String> list =new ArrayList<>();

    public SendFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SendFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SendFragment newInstance(String param1, String param2) {
        SendFragment fragment = new SendFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }



    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            //return ss.length;
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView=new TextView(getContext());
            //textView.setText(ss[position]);
            textView.setText(list.get(position));
            return textView;
        }
    }


    //初始化弹窗
    public void initDialog(){
        //设置发布按钮的点击事件
        Button btn_sendGoods = (Button) getActivity().findViewById(R.id.btn_sendGoods);
        btn_sendGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                AlertDialogUtils dialogUtils =AlertDialogUtils.getInstance();
//                AlertDialogUtils.showConfirmDialog(getContext(),"商品发布","",getActivity());
//                dialogUtils.setMonDialogButtonClickListener(new AlertDialogUtils.OnDialogButtonClickListener() {
//                    @Override
//                    public void onPositiveButtonClick(androidx.appcompat.app.AlertDialog dialog) {
//                        //点击确认按钮要做的事情
//                        System.out.println("发布商品...");
//                    }
//
//                    @Override
//                    public void onNegativeButtonClick(androidx.appcompat.app.AlertDialog dialog) {
//                        //点击取消按钮关闭弹框
//                        dialog.dismiss();
//                    }
//                });

               Intent intent = new Intent(getActivity(),DialogAddGoods.class);
               startActivity(intent);


            }
        });
    }
    //初始化已发布的商品数据和点击事件绑定
    public void initHasSendGoods(){
        Button has_send = (Button)getActivity().findViewById(R.id.has_send);
        has_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HasSendGoods.class);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDialog();
        initHasSendGoods();
      //  initImageData();
    }

    public void initImageData(){
      for (GoodVo2 g:HasSendGoods.data){

          HttpRequestUtils.getImage(g.id);
          g.setImageUrlList(HasSendGoods.imageList);
      }
    }




    //初始化下拉列表
    public void initSpiner(){
        String[] ss=new String[]{
                "北京",
                "上海",
                "深圳"
        };


        System.out.println("初始化....");
        //第二种方法
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item ,ss);

        Spinner spinner2=(Spinner) getActivity().findViewById(R.id.spinner_location);
        System.out.println("========"+spinner2);
        spinner2.setAdapter(adapter2);
//        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                //选择了不同的选项，调用这个
//                Toast.makeText(getContext(),ss[position],Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//                //在下拉选项种选择了本来选的东西，也就是说没改变选项，调用这个
//
//            }
//        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_send, container, false);
    }
}